import { PartSlaver } from './PartBase'
import { conn } from '@beshell/conn-ts/Connection'
import {AppModel} from '../AppModel'
import configLED from './config/led.json'
import configLEDRGB from './config/led-rgb.json'

export class BeLED extends PartSlaver {

    public color: string = "red"

    /**
     * int 亮度 0 - 255
     */
    private _value = 255
    public set value(v: number) {
        if(this._value == v) {
            return
        }
        this._value = v
        this.emit("value.changed", v)

        this.syncRemote()
    }
    public get value() {
        return this._value
    }

    public gammaCorrect = true

    constructor(_model: AppModel){
        super(_model, configLED)
        this._eventFilters?.push("value.changed")
    }

    positiveUsageProvider(throwError=true) {
        let pStud = this.studs[0][0]
        if( !pStud.bus ){
            if(throwError)
                throw new Error("LED正极无线路连接")
            return null
        }
        let p = pStud.bus.findHostStud()
        if(!p) {
            if(throwError)
                throw new Error("LED正极线路连接无效") 
            return null
        }
        return p
    }
    negativeUsageProvider(throwError=true) {
        let nStud = this.studs[0][1]
        if( !nStud.bus ){
            if(throwError)
                throw new Error("LED负极无线路连接")
            return null
        }
        let n = nStud.bus.findHostStud()
        if(!n) {
            if(throwError)
                throw new Error("LED负极线路连接无效")
            return null
        }
        return n
    }

    public syncRemote() {
        if(!conn.isConnected)
            return
        conn.console.run(`be.part["${this.instanceName}"].setValue(${this._value}, ${this.gammaCorrect})`)
    }
    
    public generateSetupCode(indent=0){
        return `${"\t".repeat(indent)}${this.remoteVar()} = new (require("besdk/driver/led")) ()`
    }

    public generateBeginCode(indent=0){
        let argP = 'undefined'
        let argN = ''

        let p = this.positiveUsageProvider(false)
        let n = this.negativeUsageProvider(false)

        if(p) {
            argP = p.gpio.name() || 'undefined'
        }
        if(n) {
            argN = ", " + (n.gpio.name() || 'undefined')
        }

        if(argN=='' && argP=='undefined') {
            argP = ''
        }

        return `${"\t".repeat(indent)}${this.remoteVar()}.begin(${argP}${argN})`
    }
    
    public isConnectionReady() {
        let p = this.positiveUsageProvider(false)
        let n = this.negativeUsageProvider(false)
        return !!p && !!n
    }
}


export class BeLEDRGB extends PartSlaver {

    public value = {
        r: 255 ,
        g: 255 ,
        b: 255 ,
    }

    public colorFormat() {
        return `rgb(${this.value.r},${this.value.g},${this.value.b})`
    }

    constructor(_model: AppModel){
        super(_model, configLEDRGB)
        this._eventFilters?.push("value.changed")
    }
    
    set(r:number, g:number, b:number) {
        if(this.value.r==r && this.value.g==g && this.value.b==b) {
            return
        }
        this.value.r = r
        this.value.g = g
        this.value.b = b
        this.emit("value.changed")
    }
}